package com.wa.graphql.scalar;
import graphql.schema.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.Map;

/**
 * @description: 自定义 Map Scalar
 * @author: Homan Liang
 * @time: 2021/6/16 12:46
 */
@Component
public class MapScalarType extends GraphQLScalarType {
    protected static final Logger log = LoggerFactory.getLogger(MapScalarType.class);

    public MapScalarType() {
        super("MapScalarType", "MapScalarType", new Coercing<Object, Object>() {
            @Override
            public Object serialize(Object dataFetcherResult) throws CoercingSerializeException {
                if (dataFetcherResult == null) {
                    return null;
                }

                Map map;
                try {
                    map = Map.class.cast(dataFetcherResult);
                } catch (ClassCastException exception) {
                    throw new CoercingSerializeException("Could not convert " + dataFetcherResult + " into a Map", exception);
                }
                return map;
            }

            @Override
            public Object parseValue(Object input) throws CoercingParseValueException {
                log.warn("parseValue called");
                return null;
            }

            @Override
            public Object parseLiteral(Object input) throws CoercingParseLiteralException {
                log.warn("parseLiteral called");
                return null;
            }
        });
    }
}
